package com.yyt.libsrc.helper;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.allenliu.versionchecklib.core.http.HttpParams;
import com.allenliu.versionchecklib.core.http.HttpRequestMethod;
import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.allenliu.versionchecklib.v2.builder.DownloadBuilder;
import com.allenliu.versionchecklib.v2.builder.UIData;
import com.allenliu.versionchecklib.v2.callback.ForceUpdateListener;
import com.allenliu.versionchecklib.v2.callback.RequestVersionListener;
import com.google.gson.Gson;
import com.vondear.rxtool.RxAppTool;
import com.yyt.libsrc.bean.UpdateBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.net.JavaParamsBuilder;

import top.lixb.libcommon.base.BaseApplication;
import top.lixb.libcommon.log.L;


public class UpdateHelper {


    private DownloadBuilder mDownloadBuilder;

    public void checkUpdate(Context context) {
        HttpParams httpParams = new HttpParams();
        httpParams.putAll(new JavaParamsBuilder()
                .addCmd("appInfo")
                .build());
        //强制更新
        mDownloadBuilder = AllenVersionChecker
                .getInstance()
                .requestVersion()
                .setRequestUrl(Consts.URL.SERVER+"api/json/home.do")
                .setRequestMethod(HttpRequestMethod.POST)
                .setRequestParams(httpParams)
                .request(new RequestVersionListener() {
                    @Nullable
                    @Override
                    public UIData onRequestVersionSuccess(String result) {
                        L.e("请求版本信息返回:"+result);
                        String errorstr = ",\"data\":[]";
                        if (!TextUtils.isEmpty(result) && result.contains(errorstr)) {
                            result =  result.replace(errorstr, "");
                        }
                        String error2 = ",\"version_code\":\"\"";
                        if (!TextUtils.isEmpty(result) && result.contains(error2)) {
                            result =  result.replace(error2, "");
                        }
                        String error3 = "\"data\":\"\",";
                        if (!TextUtils.isEmpty(result) && result.contains(error3)) {
                            result = result.replace(error3, "");
                        }
                        L.e(result);
                        VersionCheckBean bean = new Gson().fromJson(result, VersionCheckBean.class);

                        UIData uiData = null;
                        if (bean.success) {
                            int appVersionCode = RxAppTool.getAppVersionCode(BaseApplication.getApp());
                            if (appVersionCode < Double.valueOf(bean.data.xbb_version).intValue()) {
                                if (mDownloadBuilder != null && Integer.valueOf(bean.data.xbb_type) == 1) {
                                    //强制更新
                                    mDownloadBuilder.setForceUpdateListener(new ForceUpdateListener() {
                                        @Override
                                        public void onShouldForceUpdate() {

                                        }
                                    });
                                }
                                uiData = UIData.create();
                                uiData.setTitle("发现新版本");
                                uiData.setContent(bean.data.xbb_desc);
                                uiData.setDownloadUrl(bean.data.xbb_downurl);
                            } else {
                                L.e("APP 已经是最新版本");
                            }
                        } else {
//                            MsgHelper.showToast("获取版本信息失败," + updateBean.message);
                            L.e("获取版本信息失败,"+bean.errMsg);
                        }
                        return uiData;
                    }

                    @Override
                    public void onRequestVersionFailure(String message) {
//                        MsgHelper.showToast(message);
                        L.e("获取版本信息 failure:"+message);
                    }
                });
        mDownloadBuilder.excuteMission(context);
    }

    public static class VersionCheckBean{

        /**
         * success : true
         * data : {"xbb_version":"1.0","xbb_name":"熊宝呗app","xbb_type":"1","xbb_downurl":"http://admin.huiyie.com/app/xiongbaobei.apk","xbb_desc":"熊宝呗app新版本上线啦"}
         * errCode : SUCCESS
         * errMsg :
         * sysTime : 1535034734050
         */

        public boolean success;
        public DataBean data;
        public String errCode;
        public String errMsg;
        public long sysTime;

        public static class DataBean {
            /**
             * xbb_version : 1.0
             * xbb_name : 熊宝呗app
             * xbb_type : 1
             * xbb_downurl : http://admin.huiyie.com/app/xiongbaobei.apk
             * xbb_desc : 熊宝呗app新版本上线啦
             */

            public String xbb_version;
            public String xbb_name;
            public String xbb_type;
            public String xbb_downurl;
            public String xbb_desc;
        }
    }
}
